package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.IAddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private IAddressBookService addressBookService;

    @PostMapping
    public R saveAddress(@RequestBody AddressBook addressBook){
        log.info("saveAddress");
        addressBook.setUserId(BaseContext.getThreadLocal());
        addressBookService.save(addressBook);

        return R.success("保存地址成功");
    }


    @PutMapping("/default")
    public R setDefault(@RequestBody AddressBook addressBook){
        log.info("setDefault");
        //先设置该用户所有默认地址为0
        LambdaUpdateWrapper<AddressBook> lqw = new LambdaUpdateWrapper<>();
        lqw.set(AddressBook::getIsDefault,0).eq(AddressBook::getUserId, BaseContext.getThreadLocal());
        addressBookService.update(lqw);
        //将当前设置的默认地址改为1
        LambdaUpdateWrapper<AddressBook> lqw2 = new LambdaUpdateWrapper<>();
        lqw2.set(AddressBook::getIsDefault,1).eq(AddressBook::getId,addressBook.getId());
        addressBookService.update(lqw2);

        return R.success("设置成功");

    }


    @GetMapping("/{id}")
    public R selectOne(@PathVariable Long id){
        log.info("getById");
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook!=null){
            return R.success(addressBook);
        }
        return R.error("没有找到对象");
    }

    @GetMapping("/default")
    public R selectDefault(){
        log.info("getDefault");
        LambdaQueryWrapper<AddressBook> qw=new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,BaseContext.getThreadLocal()).eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(qw);
        return R.success(one);

    }

    @GetMapping("/list")
    public R selectList(){
        log.info("getListByUser");
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,BaseContext.getThreadLocal());
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);
    }

    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){


        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }

    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook){
        //判断哪些不用
        addressBook.setUserId(BaseContext.getThreadLocal());

        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.eq(addressBook.getId()!=0,AddressBook::getId,addressBook.getId());

        addressBookService.update(addressBook,qw);
        return R.success("修改成功");

    }
}
